package com.video.service.impl;

import com.alibaba.fastjson.JSON;
import com.api.client.IdClient;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.context.UserContext;
import com.common.domain.po.LikedRecord;
import com.common.domain.po.Video;
import com.common.domain.po.VideoBrowseRecord;
import com.common.domain.po.VideoItem;
import com.common.exception.CodeAndMsg;
import com.common.exception.JavaSmException;
import com.common.utils.BeanUtils;
import com.common.utils.Convert;
import com.video.mapper.VideoItemMapper;
import com.video.mapper.VideoMapper;
import com.video.service.VideoItemService;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 路亚
 * @version 0.1
 * @className VideoItemServiceImpl
 * @description
 * @date 2024/12/13 15:28
 * @since jdk11
 */
@Service
@RequiredArgsConstructor
public class VideoItemServiceImpl extends ServiceImpl<VideoItemMapper, VideoItem> implements VideoItemService {

    private final VideoItemMapper videoItemMapper;

    private final VideoMapper videoMapper;

    private final IdClient idClient;

    private final DefaultMQProducer defaultMQProducer;

    private static final String VIDEO_BROWSE_RECORD_TYPE = "video_browse_record";

    @Override
    public void removeByVideoId(Long id) {
        videoItemMapper.deleteByVideoId(id);
    }

    @Override
    public void playBack(Long id) {
        // 组装视频播放记录对象，发送mq给播放服务，然后播放服务更新数据库
        VideoItem videoItem = videoItemMapper.selectById(id);
        Video video = videoMapper.selectById(videoItem.getVideoId());
        // 拷贝属性
        VideoBrowseRecord videoBrowseRecord = BeanUtils.copyBean(video, VideoBrowseRecord.class, new Convert<Video, VideoBrowseRecord>() {
            @Override
            public void convert(Video source, VideoBrowseRecord target) {
                target.setId(idClient.getLongKeyInterval(VIDEO_BROWSE_RECORD_TYPE));
                target.setUserId(UserContext.getCurrentId());
                target.setVideoId(video.getId());
                target.setVideoName(video.getName());
                target.setVideoImageUrl(video.getCoverUrl());
                target.setVideoItemId(videoItem.getId());
                target.setVideoItemNum(videoItem.getNum());
                target.setVideoItemUrl(videoItem.getVideoUrl());
                target.setViewTime(LocalDateTime.now());
            }
        });
        // 发送mq
        Message message = new Message(
                "videoBrowseRecord",
                "videoBrowseRecord_add",
                JSON.toJSONString(videoBrowseRecord).getBytes()
        );
        try {
            defaultMQProducer.send(message);
        } catch (Exception e) {
            throw new JavaSmException(CodeAndMsg.MQ_SEND_ERROR);
        }
    }

    @Override
    public LikedRecord queryByVideoItemId(Long id) {
        VideoItem videoItem = videoItemMapper.selectById(id);
        Video video = videoMapper.selectById(videoItem.getVideoId());
        return new LikedRecord(
                null,
                null,
                video.getId(),
                video.getName(),
                video.getCoverUrl(),
                videoItem.getId(),
                videoItem.getNum(),
                videoItem.getVideoUrl(),
                LocalDateTime.now()
        );
    }
}
